package com.ruoyi.business.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.ruoyi.framework.config.FastJson2JsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class RedisConfig2 {

    @Autowired
    private Environment env;


    @Bean
    public RedisTemplate<Object, Object> redisTemplate2(@Qualifier("connectionFactory2") RedisConnectionFactory connectionFactory2) {

        RedisTemplate<Object, Object> redisTemplate2 = new RedisTemplate<>();
        redisTemplate2.setConnectionFactory(connectionFactory2);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        serializer.setObjectMapper(mapper);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        redisTemplate2.setKeySerializer(new StringRedisSerializer());
        redisTemplate2.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        redisTemplate2.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate2.setHashValueSerializer(serializer);

        redisTemplate2.afterPropertiesSet();

        return redisTemplate2;
    }

    @Bean
    @Primary
    public RedisConnectionFactory connectionFactory() {
        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
        connectionFactory.setHostName(env.getProperty("spring.redis.host"));
        connectionFactory.setPort(Integer.parseInt(env.getProperty("spring.redis.port")));
        connectionFactory.setPassword(env.getProperty("spring.redis.password"));
        connectionFactory.setDatabase(Integer.parseInt(env.getProperty("spring.redis.database")));
        return connectionFactory;
    }

    @Bean
    public RedisConnectionFactory connectionFactory2() {
        LettuceConnectionFactory connectionFactory2 = new LettuceConnectionFactory();
        connectionFactory2.setHostName(env.getProperty("spring.redis2.host"));
        connectionFactory2.setPort(Integer.parseInt(env.getProperty("spring.redis2.port")));
        connectionFactory2.setPassword(env.getProperty("spring.redis2.password"));
        connectionFactory2.setDatabase(Integer.parseInt(env.getProperty("spring.redis2.database")));
        return connectionFactory2;
    }
}